package com.example.voice
import android.content.Context;
import androidx.annotation.NonNull
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import mobile.Mobile
import kotlinx.coroutines.*
import java.lang.Exception
import android.content.Intent;
/** VoicePlugin */
class VoicePlugin : FlutterPlugin, MethodCallHandler {

    private lateinit var applicationContext: Context
    private lateinit var methodChannel: MethodChannel
    private var serviceIntent: Intent? = null
    override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
        applicationContext = flutterPluginBinding.getApplicationContext();
        methodChannel = MethodChannel(flutterPluginBinding.binaryMessenger, "voice")
        methodChannel.setMethodCallHandler(this)
    }



    override fun onMethodCall(call: MethodCall, result: Result) {
        if (call.method == "getPlatformVersion") {
            result.success("Android ${android.os.Build.VERSION.RELEASE}")
        } else if (call.method == "run") {
            var port: String? = call.argument<String>("port")
            var name: String? = call.argument<String>("name")
            var ip: String? = call.argument<String>("ip")
            if (serviceIntent == null) {
                // 将参数放入Intent中
                serviceIntent = Intent(applicationContext, FrpService::class.java)
                // 将参数放入Intent中
                serviceIntent?.putExtra("port", port)
                serviceIntent?.putExtra("name", name)
                serviceIntent?.putExtra("ip", ip)
                applicationContext.startService(serviceIntent)
            }
            result.success("ok");
        } else {
            result.notImplemented()
        }
    }

    override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
        methodChannel.setMethodCallHandler(null)
    }
}
